package com.yf.admin.service;

import com.yf.admin.dao.RoleMapper;
import com.yf.admin.dao.UserMapper;
import com.yf.admin.domain.LoggedUser;
import com.yf.admin.domain.Role;
import com.yf.admin.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuthenticationService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userMapper.loadUserByName(s);
        if(user==null){
            throw new UsernameNotFoundException("账号:"+s+"不存在!");
        }
        List<Role> roles = roleMapper.findByUser(user);
        return new LoggedUser(user,roles);
    }
}
